﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationGenerator.DataAccess;
using ApplicationGenerator.Versions.BusinessLogic;

namespace GUserInterface
{

    public delegate void TreeViewClickedHandler(string FormTitle, ApplicationGenerator.DataAccess.Policy policy);


    public partial class SolutionExplorer : GToolbox
    {

        #region Events

        public event TreeViewClickedHandler TreeViewClickedEvent;

        #endregion

        #region Construction

        public SolutionExplorer()
        {
            InitializeComponent();
            InitializeComponent_Custom();
            TabText = "Projekt";
        }

        private void InitializeComponent_Custom()
        {

        }

        #endregion

        #region Methods

        protected override void OnRightToLeftLayoutChanged(EventArgs e)
        {
            treeView1.RightToLeftLayout = RightToLeftLayout;
        }

        public void AddSolutionNode(string title, ApplicationGenerator.DataAccess.Policy policy)
        {
            //add to Forms in solution explorer
            switch (policy)
            {
                case ApplicationGenerator.DataAccess.Policy.Form:
                    treeView1.Nodes[0].Nodes[0].Nodes.Add(title);
                    break;
                case ApplicationGenerator.DataAccess.Policy.List:
                    treeView1.Nodes[0].Nodes[1].Nodes.Add(title);
                    break;
                case ApplicationGenerator.DataAccess.Policy.Workflow:
                    treeView1.Nodes[0].Nodes[2].Nodes.Add(title);
                    break;
            }

        }

        /// <summary>
        /// returns number of saved forms
        /// </summary>
        /// <returns>number of saved forms</returns>
        public int GetNumOfDocuments(Policy policy)
        {
            if (policy == Policy.Form)
            {
                return treeView1.Nodes[0].Nodes[0].Nodes.Count;
            }
            else if (policy == Policy.List)
            {
                return treeView1.Nodes[0].Nodes[1].Nodes.Count;
            }
            else
            {
                return treeView1.Nodes[0].Nodes[2].Nodes.Count;
            }
        }


        /// <summary>
        /// returns number of saved lists
        /// </summary>
        /// <returns>number of saved lists</returns>
        public int GetNumOfLists()
        {
            return treeView1.Nodes[0].Nodes[1].Nodes.Count;
        }

        #endregion

        public void LoadSolution()
        {
            //load current forms from database and fill treeview
            //List<string> names = BusinessModule.DAL.DesignerDataBaseManager.Instance.GetFormNames();
            VersionDataAccess formDataAccess = new VersionDataAccess(VersionElement.Form);
            VersionDataAccess listDataAccess = new VersionDataAccess(VersionElement.List);
            VersionDataAccess flowDataAccess = new VersionDataAccess(VersionElement.Workflow);
            IList<VersionInfo> formList = formDataAccess.GetVersions();
            IList<VersionInfo> flowList = flowDataAccess.GetVersions();
            IList<VersionInfo> listList = listDataAccess.GetVersions();

            if (null != formList)
            {
                for (int i = 0; i < formList.Count; i++)
                {
                    treeView1.Nodes[0].Nodes[0].Nodes.Add(formList[i].Name);
                }
            }

            if (null != listList)
            {
                for (int i = 0; i < listList.Count; i++)
                {
                    treeView1.Nodes[0].Nodes[1].Nodes.Add(listList[i].Name);
                }
            }

            if (null != flowList)
            {
                for (int i = 0; i < flowList.Count; i++)
                {
                    treeView1.Nodes[0].Nodes[2].Nodes.Add(flowList[i].Name);
                }
            }

        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            TreeView treeView = sender as TreeView;
            TreeNode treeNode = treeView.SelectedNode;

            if (treeNode == null)
            {
                return;
            }

            string formTitle = treeNode.Text;

            if (formTitle == "Root" ||
                formTitle == "Formularze" ||
                formTitle == "Structures" ||
                formTitle == "Procesy" ||
                formTitle == "Listy")
            {
                return;
            }



            if (!string.IsNullOrEmpty(formTitle))
            {
                if (treeNode.Parent.Text == "Formularze")
                {
                    TreeViewClickedEvent(formTitle, Policy.Form);
                }
                else if (treeNode.Parent.Text == "Listy")
                {
                    TreeViewClickedEvent(formTitle, Policy.List);
                }
                else if (treeNode.Parent.Text == "Procesy")
                {
                    TreeViewClickedEvent(formTitle, Policy.Workflow);
                }
                else //MENU
                {
                    TreeViewClickedEvent(formTitle, Policy.Menu);
                }
            }
        }
    }
}
